package com.open.system.controller;

import java.util.Date;	
import java.util.List;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.open.system.domain.LoanContactDO;
import com.open.system.service.LoanAdminLogService;
import com.open.system.service.LoanApplyLogService;
import com.open.system.service.LoanContactService;
import com.open.common.controller.BaseController;
import com.open.common.utils.PageUtils;
import com.open.common.utils.Query;
import com.open.common.utils.R;

/**
 * 联系人
 */
 
@Controller
@RequestMapping("/system/loanContact")
public class LoanContactController extends BaseController {
	@Autowired
	private LoanContactService loanContactService;
	
	@Autowired
	private LoanApplyLogService loanApplyLogService;
	
	@Autowired
	private LoanAdminLogService loanAdminLogService;
	
	@GetMapping()
	@RequiresPermissions("system:loanContact:loanContact")
	String LoanContact(){
	    return "system/loanContact/loanContact";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:loanContact:loanContact")
	public PageUtils list(@RequestParam Map<String, Object> params){
		String shopId=getUser().getEmail();//店铺ID
		if(shopId!=null && shopId!="" && !"1".equals(shopId)){
			params.put("shopId",shopId);
		}
		//查询列表数据
        Query query = new Query(params);
        List<LoanContactDO> loanContactList=null;
        int total=0;
        if("1".equals(shopId)){
        	loanContactList= loanContactService.list(query);
        	total = loanContactService.count(query);
        }else {
        	loanContactList = loanContactService.list2(query);
    		total = loanContactService.count2(query);
		}
		PageUtils pageUtils = new PageUtils(loanContactList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:loanContact:add")
	String add(){
	    return "system/loanContact/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:loanContact:edit")
	String edit(@PathVariable("id") Long id,Model model){
		LoanContactDO loanContact = loanContactService.get(id);
		model.addAttribute("loanContact", loanContact);
	    return "system/loanContact/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:loanContact:add")
	public R save( LoanContactDO loanContact){
		loanContact.setCreateTime(new Date());
		if(loanContactService.save(loanContact)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:loanContact:edit")
	public R update( LoanContactDO loanContact){
		loanContactService.update(loanContact);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:loanContact:remove")
	public R remove( Long id){
		if(loanContactService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:loanContact:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		loanContactService.batchRemove(ids);
		return R.ok();
	}
	
}
